package com.api.service.v1;

import com.admin.entity.Apk;
import com.admin.mapper.ApkMapper;
import com.api.bean.ApkBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.ResponseBean;
import com.util.MD5Util;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApkAPIService {

	@Value("${SERVER}")
	private String SERVER;
	@Autowired
	private ApkMapper apkDao;

	public ResponseBean findapk() throws IOException {
		
		String url = SERVER;// "http://192.168.2.156:8080/wo-web";

		Resource resource = new ClassPathResource("");
		String ROOT_PATH_NEW = resource.getFile().getAbsolutePath();
		System.out.println(ROOT_PATH_NEW);
		QueryWrapper q = new QueryWrapper();
		q.eq("apk_is_it_public","公开");
		List<Apk> apks=apkDao.selectList(q);
		List<ApkBean> list=new ArrayList<ApkBean>();
		if(apks==null || apks.size()==0){
			
		}else{
			for (Apk apk : apks) {
				ApkBean apkBean=new ApkBean();
				String name = apk.getApkName();
				if (StringUtil.isNotNull(name))
					apkBean.setName("");
				else
					apkBean.setName("");

				if (StringUtil.isNotNull(name))
					apkBean.setApkName(name);
				else
					apkBean.setApkName("");

				if (StringUtil.isNull(apk.getApkPictureUrl())) {
					apkBean.setImage(null);
				} else {
					String md5 = MD5Util.md5sum(ROOT_PATH_NEW + apk.getApkPictureUrl());
					apkBean.setImage(url + apk.getApkPictureUrl() + "?md5=" + md5);
				}

				apkBean.setPackageName(apk.getApkPackage());

				if (StringUtil.isNull(apk.getApkUrl())) {
					apkBean.setPackageUrl(null);
				} else {
					String md5 = MD5Util.md5sum(ROOT_PATH_NEW + apk.getApkUrl());
					apkBean.setPackageUrl(url + apk.getApkUrl() + "?md5=" + md5);
				}
				list.add(apkBean);
			} 
		}
		return new ResponseBean(200, "成功", list);
	}
}
